package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

class RotateContentView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.black)
            textSize = context.resources.getDimension(R.dimen.sp_20)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_B128C9)
            textSize = context.resources.getDimension(R.dimen.sp_20)
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_ff5722)
            textSize = context.resources.getDimension(R.dimen.sp_20)
        }
    }

    private var mWidth = 0
    private var mHeight = 0

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        mWidth = when (MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when (MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            save()
            rotate(20f)
            drawText("我们", 200f, 50f, mPaint0)
            restore()

            save()
            rotate(90f)
            drawText("我们", 30f, -300f, mPaint1)
            restore()

            save()
            val degrees = 45f
            val px2 = 100f
            val py2 = 50f
            //旋转平移操作原理:先旋转,再平移.
            //画布旋转平移后会形成新的坐标系,绘制内容也只在新坐标系中绘制,不会再去映射原坐标系.
            rotate(degrees)
            //平移遵循上加下减、左加右减原则
            translate(px2, py2)
            val text3X = 75f - px2
            val text3Y = 45f - py2
            drawText("我们", text3X, text3Y, mPaint2)
            restore()

            //save()
            //val degrees3 = 45f
            //val px3 = 100f
            //val py3 = 50f
            //rotate(degrees3, px3, py3)
            //val xRotateValue = sin(degrees / 180f * Math.PI)
            //val yRotateValue = cos(degrees / 180f * Math.PI)
            //val text3X = (((px2 * xRotateValue - px2) * xRotateValue).toFloat() + (200f + px2) * xRotateValue).toFloat()
            //val text3X = px2 - 350f * 2
            //val text3X = (-abs(2 * py2) - 220f)
            //val text3Y = (((py2 * yRotateValue - py2) * yRotateValue).toFloat() + 60f * yRotateValue).toFloat()
            //restore()
        }
    }
}